from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StringType, IntegerType,DoubleType
import pandas as pd
from pyspark.sql import functions as F

# 5 月销量排行(wan)
if __name__ == '__main__':
    spark = SparkSession.builder. \
        appName("xixi4494"). \
        getOrCreate()
    sc = spark.sparkContext

    # JSON类型自带有Schema信息
    df = spark.read.format("json").load("hdfs://node1:8020/input/mini.json")
    df.groupBy(F.from_unixtime(df['dateTS'].substr(0, 10), "MM").alias("month")). \
        count(). \
        orderBy("count", ascending=False). \
        write.mode("overwrite"). \
        format("jdbc"). \
        option("url", "jdbc:mysql://node1:3306/bigdata?useSSL=false&useUnicode=true"). \
        option("dbtable", "monthrank"). \
        option("user", "root"). \
        option("password", "123456"). \
        save()
